#!/bin/bash
#
# piRTC
#
# Setup RTC
#
#

BINDIR=$(cd $(dirname $0) && pwd)

. ${BINDIR}/common


RTCDEVICE="/dev/rtc0"
if [ "${FPPPLATFORM}" = "BeagleBone Black" ]; then
    if [ -f "/sys/class/rtc/rtc0/name" ]; then
        RTCNAME=$(</sys/class/rtc/rtc0/name)
        if [[ $RTCNAME == *"omap_rtc"* ]]; then
            RTCDEVICE="/dev/rtc1"
        fi
        # Beaglebone Green Gateway has a ds1307 on i2c0
        # but no battery (by default) so cape would
        # be rtc1 and that's what we should use
        if [[ $RTCNAME == *" 0-00"* ]]; then
            RTCDEVICE="/dev/rtc1"
        fi
    fi
fi

#############################################################################
# Setup RTC on the Pi.
setupRTC () {

    if  [ "${FPPPLATFORM}" = "Raspberry Pi" ]; then
        # RTC Setup
        # to get the RPi board version:
        REV=$(cat /proc/cpuinfo|grep Revision)
        # Extract revision
        REV=${REV##*: }
        # Make it readable as hex
        REV=0x$REV
        # Convert hex to dec
        REV=$((REV))

        if [ $REV -lt 4 ]; then
            I2CBUS="i2c-0"
        else
            I2CBUS="i2c-1"
        fi
    elif [ "${FPPPLATFORM}" = "BeagleBone Black" ]; then
        I2CBUS="i2c-2"
    else
        return
    fi

    # Read Clock Setting From Settings File
    piRTC=$(awk -f ${FPPDIR}/scripts/readSetting.awk ${SETTINGSFILE} setting=piRTC)

    case "$piRTC" in
    1)
      # rasClock chip
      echo "FPP - Configuring RTC, Setting to rasClock/${I2CBUS}"
      echo "pcf2127 0x51" > /sys/bus/i2c/devices/${I2CBUS}/new_device
      sleep 1
      ;;
    2)
      # DS1307 chip
      echo "FPP - Configuring RTC, Setting to DS1307/${I2CBUS}"
      echo "ds1307 0x68" > /sys/bus/i2c/devices/${I2CBUS}/new_device
      sleep 1
      ;;
    3)
      # PiFace Chip
      echo "FPP - Configuring RTC, Settting to mcp7941x/${I2CBUS}"
      echo "mcp7941x 0x6f" > /sys/bus/i2c/devices/${I2CBUS}/new_device
      sleep 1
      ;;
    4)
      # AdaFruit
      echo "FPP - Configuring RTC, Settting to pcf8523/${I2CBUS}"
      echo "pcf8523 0x68" > /sys/bus/i2c/devices/${I2CBUS}/new_device
      sleep 1
      ;;
    5)
      # pcf85363
      echo "FPP - Configuring RTC, Settting to pcf85363/${I2CBUS}"
      echo "pcf85363 0x51" > /sys/bus/i2c/devices/${I2CBUS}/new_device
      sleep 1
      ;;
    *)
      # None
      echo "FPP - Configuring RTC, None Selected"
      ;;
    esac
}

if [ ! -e ${RTCDEVICE} ]; then
    setupRTC
fi

if [ -e ${RTCDEVICE} ]; then
    if [[ $1 == "set" ]]
    then
      hwclock -w -f ${RTCDEVICE}
    else
      hwclock -s -f ${RTCDEVICE}

      if [ "${FPPPLATFORM}" = "BeagleBone Black" ]; then
          # set the built in rtc to the same time as read from the RTC
          if [ "${RTCDEVICE}" != "/dev/rtc0" ]; then
            hwclock -w -f /dev/rtc0
          fi
      fi
      cp ${FPPDIR}/etc/update-RTC /etc/cron.daily/
    fi

    date
fi
